//
//  THMaskStackViewController.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THMaskStackViewController.h"
#import "THNavigationController.h"
@interface THMaskStackViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic,assign) BOOL isCreate;
@property (nonatomic,assign) CGSize frameSize;
@property (nonatomic,retain,readwrite) UIView *bgView;
@property (nonatomic,retain) UIView *contentView;
@end

@implementation THMaskStackViewController

- (void)dealloc{
    NSLog(@"THMaskStackViewController dealoc");
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (instancetype)initWithContentView:(UIView*)contentView
                               size:(CGSize)size{
    if (self = [super init]) {
        _frameSize = size;
        _contentView = contentView;
    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!_isCreate) {
        [self show];
        _isCreate = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self commit];
}
- (void)configViews{
    [super configViews];
    [self.view setBackgroundColor:[UIColor clearColor]];
}
- (void)commit{
    self.duration      = 0.2;
    self.enableTouch   = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handleGesture:)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
}
- (void)show{
    self.bgView = [[UIView alloc] initWithFrame:CGRectZero];
    self.bgView.backgroundColor = [ColorFromHex(0x333333) colorWithAlphaComponent:0.5];
    [self.view addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    [self.bgView addSubview:self.contentView];
    [self.view setNeedsLayout];
    self.contentView.frame = [self prepareFrame];
    self.view.userInteractionEnabled = NO;
    [UIView animateWithDuration:self.duration delay:0 usingSpringWithDamping:0.8
            initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveLinear
             animations:^{
        self.contentView.frame = [self didFinishFrame];
    } completion:^(BOOL finished) {
        self.view.userInteractionEnabled = YES;
    }];
}
- (void)dismiss{
    [UIView animateWithDuration:self.duration delay:0
    options:UIViewAnimationOptionCurveLinear animations:^{
        self.contentView.frame = [self prepareFrame];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{}];
    }];
}
- (CGRect)prepareFrame{
    return CGRectMake((CGRectGetWidth(self.view.frame) - CGRectGetWidth(self.contentView.frame))*0.5,CGRectGetHeight(self.view.frame), CGRectGetWidth(self.contentView.frame),CGRectGetHeight(self.contentView.frame));
}
- (CGRect)didFinishFrame{
     return CGRectMake((CGRectGetWidth(self.view.frame) - CGRectGetWidth(self.contentView.frame))*0.5, ((CGRectGetHeight(self.view.frame)-CGRectGetHeight(self.contentView.frame)))*0.5, CGRectGetWidth(self.contentView.frame),CGRectGetHeight(self.contentView.frame));
}
- (void)handleGesture:(UIGestureRecognizer*)gestureRecognizer{
    CGPoint touchPoint = [gestureRecognizer locationInView:self.view];
    if (!self.enableTouch) return;
    if (CGRectContainsPoint(self.contentView.frame,touchPoint)) return;
    [self dismiss];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:_contentView]) return NO;
    return YES;
}
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer.view isDescendantOfView:_contentView]) return NO;
    return YES;
}
- (void)showFromVC:(UIViewController*)vc{
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [vc presentViewController:self animated:NO completion:^{}];
}
- (void)showFromNavVC:(UIViewController*)vc{
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    THNavigationController *nav = [[THNavigationController alloc] initWithRootViewController:self];
    nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [vc presentViewController:nav animated:NO completion:^{}];
}
- (void)disMiss{
    [self dismissViewControllerAnimated:NO completion:^{}];
}
- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationOverCurrentContext;
}
@end
